package com.uniriho.setternumber

import android.annotation.SuppressLint
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import android.util.Log
import android.view.Window
import android.widget.Toast
import androidx.activity.ComponentActivity
import com.uniriho.setternumber.databinding.ActivityMainBinding
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext


class MainActivity : ComponentActivity() {
    private val mainBinding by lazy {
        ActivityMainBinding.inflate(layoutInflater)
    }

    var mFilter: IntentFilter? = null

    private val mReceiver: BroadcastReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            val scanData = intent.getStringExtra("EXTRA_SCAN_DATA")
            if (scanData != null) {
                Log.e("scanLen", scanData.length.toString() + "")
                Log.e("scanData", scanData)
                if (scanData.startsWith("F")) {
                    mainBinding.editDeNo.setText(scanData)
                }else if (scanData.startsWith("DZ4")) {
                    mainBinding.editTerNo.setText(scanData)
                }else{
                    Toast.makeText(this@MainActivity,"编码异常，请检查!!!",Toast.LENGTH_SHORT).show()
                }

            }
        }
    }

    private val mainMoudel by lazy {
        MainMoudel(mainBinding,this)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        setContentView(mainBinding.root)
        initEvent()
        initData()
    }
    @SuppressLint("SetTextI18n")
    private fun initData() {
        mainBinding.textSamNo.setText("获取中...")
        mFilter = IntentFilter("ACTION_BAR_SCAN")
        registerReceiver(mReceiver, mFilter)
        val intent = Intent("android.intent.scanner.CONFIG")
        val state = 1001
        intent.putExtra("state", state)
        sendBroadcast(intent)
        mainMoudel.initData()
        CoroutineScope(Dispatchers.IO).launch {
            val samSN = mainMoudel.getSamSN()
            val flashID = mainMoudel.getFlashID()
            withContext(Dispatchers.Main){
                mainBinding.textSamNo.setText("SamNo:${samSN}\nFlash:$flashID")
            }
        }
    }

    @SuppressLint("SetTextI18n")
    private fun initEvent() {
        mainBinding.btnWrite.setOnClickListener {
            mainMoudel.checkTerNo_DeNo_write()
        }

        mainBinding.btnRead.setOnClickListener {
            mainBinding.editDeNo.setText("")
            mainBinding.editTerNo.setText("")
            mainMoudel.getDeNo()
            mainMoudel.getTerNo()
        }

        mainBinding.btnWriteDeNUm.setOnClickListener{
            mainMoudel.checkDeNo_write()
        }

        mainBinding.btnWriteTerNum.setOnClickListener {
            mainMoudel.checkTerNo_write()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        val intent = Intent("android.intent.scanner.CONFIG")
        val state = 1000
        intent.putExtra("state", state)
        sendBroadcast(intent)
        unregisterReceiver(mReceiver)
        mainMoudel.onDestroy()
    }
}